package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.GroupMapper;
import com.csun.cmny.provider.dao.OmcMonitorLogMapper;
import com.csun.cmny.provider.dto.OmcMonitorLogQueryDto;
import com.csun.cmny.provider.model.dto.user.OmcUserQueryDto;
import com.csun.cmny.provider.model.vo.OmcUserVo;
import com.csun.cmny.provider.pojo.OmcMonitorLog;
import com.csun.cmny.provider.service.OmcMonitorLogService;
import com.csun.cmny.provider.service.UacRpcService;
import com.csun.cmny.provider.vo.OldManMVo;
import com.csun.cmny.provider.vo.OmcMonitorLogVo;
import com.github.pagehelper.PageHelper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cehnzheng
 * @description:
 * @date 2019/7/18 16:52
 */
@Slf4j
@Data
@Service
@Transactional
public class OmcMonitorLogServiceImpl extends BaseService<OmcMonitorLog> implements OmcMonitorLogService {
    @Resource
    private OmcMonitorLogMapper omcMonitorLogMapper;

    @Resource
    private UacRpcService uacRpcService;

    @Resource
    private GroupMapper groupMapper;

    /**
     *
     * 新增或者修改班长日志
     *
     * @param omcMonitorLog
     * @param loginAuthDto
     * @return
     */
    @Override
    public int saveMonitorLog(OmcMonitorLog omcMonitorLog, LoginAuthDto loginAuthDto) {
        omcMonitorLog.setUpdateInfo(loginAuthDto);
        if (omcMonitorLog.isNew()) {//新增班长日志
            return omcMonitorLogMapper.insertSelective(omcMonitorLog);
        } else {//跟新班长日志
            return omcMonitorLogMapper.updateByPrimaryKeySelective(omcMonitorLog);
        }
    }

    /**
     * 查询班长日志
     *
     * @param omcMonitorLogQueryDto
     * @return
     */
    @Override
    public List<OmcMonitorLogVo> queryMonitorLogList(OmcMonitorLogQueryDto omcMonitorLogQueryDto) {
//        PageHelper.startPage(omcMonitorLogQueryDto.getPageNum(), omcMonitorLogQueryDto.getPageSize());
        //查询班长日志
        List<OmcMonitorLogVo> list = omcMonitorLogMapper.queryMonitorLogList(omcMonitorLogQueryDto);
        return list;
    }

    /**
     * 通过姓名模糊查询老人信息
     *
     * @param omcMonitorLogQueryDto
     * @return
     */
    @Override
    public List<OldManMVo> selectOldmanBynameM(OmcMonitorLogQueryDto omcMonitorLogQueryDto) {
        return omcMonitorLogMapper.selectOldmanBynameM(omcMonitorLogQueryDto);
    }

    /**
     * 根据姓名模糊查询负责人
     *
     * @param omcMonitorLogQueryDto
     * @return
     */
    @Override
    public List<OmcUserVo> selectUserByName(OmcMonitorLogQueryDto omcMonitorLogQueryDto) {
        Long id = omcMonitorLogQueryDto.getNursingGroupId() == null ? 0 : omcMonitorLogQueryDto.getNursingGroupId();
        //获取护工组绑定的护工ID
        List<Long> userIdList = groupMapper.selectUserIdListById(id);
        OmcUserQueryDto omcUserQueryDto = new OmcUserQueryDto();
        omcUserQueryDto.setName(omcMonitorLogQueryDto.getName());
        omcUserQueryDto.setRoleCode("nursing");//护工角色code
        omcUserQueryDto.setUserIdList(userIdList);
        List<OmcUserVo> list = uacRpcService.queryOmcUserListByQueryDto(omcUserQueryDto);
        return list;
    }
}
